package cn.wolfcode.luowowo.mgrsite.job;

/**
 * Created by LENOVO on 2019/8/15.
 */

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.vo.StrategyStatisPersistenceVO;
import cn.wolfcode.luowowo.article.vo.TravelStatisPersistenceVO;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.until.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * 定时器,定时执行redis数据的持久化
 */
//@Component
public class RedisDataPersistenceJob {


    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IStrategyDetailService strategyDetailService;


    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelStatisRedisService travelStatisRedisService;





    @Scheduled(cron = "0/20 * * * * ? ")
    public void dosomething(){
        System.out.println("-------------Persistence--------begin");
        //reddis命令 : 通过指定的可以表达式,获取所有的vo对象

        List<StrategyStatisVO> vos =strategyStatisRedisService.getStrategyStatisVos(RedisKeys.STRATEGY_STATIS_VO.getPrefix());
            for (StrategyStatisVO vo :vos){
                //new Sta
                StrategyStatisPersistenceVO pvo = new StrategyStatisPersistenceVO();

                try {
                    BeanUtils.copyProperties(pvo,vo);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                //执行update操作
                strategyDetailService.updateStatisVO(pvo);
            }

        List<TravelStatisVO> travelStatisVos = travelStatisRedisService.getTravelStatisVos(RedisKeys.TRAVEL_STATIS_VO.getPrefix());
            for (TravelStatisVO travelStatisVO :travelStatisVos){
                TravelStatisPersistenceVO tvo =new TravelStatisPersistenceVO();
                try {
                    BeanUtils.copyProperties(tvo,travelStatisVO);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }

                //执行update操作
                travelService.updateStatisVO(tvo);
            }


        System.out.println("-------------Persistence--------end");
    }
}
